package org.qloudgen.txn.dilithium.cluster.security;

import org.apache.ibatis.session.SqlSession;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormValidatingBehavior;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.validation.validator.StringValidator;
import org.qloudgen.txn.dilithium.cluster.beans.UserBean;
import org.qloudgen.txn.dilithium.cluster.sqlmap.ConfigNames;

import org.qloudgen.txn.dilithium.cluster.common.CommonValues;

@SuppressWarnings("rawtypes")
public class CreateUserPage extends WebPage{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private PageParameters pm = new PageParameters() ;
	
	@SuppressWarnings("unchecked")
	public CreateUserPage(){
		
		super();
		
		Form form = new Form("form"){
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit() {
				UserBean user = (UserBean) getDefaultModelObject();
				
				if (!user.getUserPassword().equals(user.getUserRePassword())){
					error(CommonValues.ERROR_TWO_PASSWORDS_MISMATCHER);
				} else {
					SqlSession sess = CommonValues.factory.openSession(true);
					
					if (sess.selectOne(ConfigNames.IS_EXISTS_USER,user) == null){
						sess.insert(ConfigNames.INSERT_USER, user);
						sess.close();
						setResponsePage(getApplication().getHomePage());
					} else {
						error(CommonValues.ERRO_CREATE_USRE);
						sess.close();
					}
				}
			}
		};
		form.setOutputMarkupId(true);
		add(form);
		UserBean user = new UserBean();
		form.setDefaultModel(new CompoundPropertyModel(user));	

		final FeedbackPanel feedback = new FeedbackPanel("feedback");
		feedback.setOutputMarkupId(true);
		form.add(feedback);
		
		RequiredTextField userNo = new RequiredTextField("userNo");
		userNo.add(StringValidator.minimumLength(4));
		userNo.setOutputMarkupId(true );
		form.add(userNo);
		
		AjaxFormValidatingBehavior behavior = new AjaxFormValidatingBehavior(form,"submit") {
				/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

				@Override
				protected void onError(AjaxRequestTarget target) {
				target.add(feedback);
				super.onError(target);
				}
		};
				
		userNo.add(behavior);
		
		form.add(new PasswordTextField("userPassword"));
		form.add(new PasswordTextField("userRePassword"));
		
		Button cancel = new Button ("cancel"){
			private static final long serialVersionUID = 1L;
			public void onSubmit() {
				setResponsePage(getApplication().getHomePage());
			}
		};
		form.add(cancel);
		cancel.setDefaultFormProcessing(false);
	}

	public PageParameters getPm() {
		return pm;
	}

	public void setPm(PageParameters pm) {
		this.pm = pm;
	}
	
	
}
